﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;

namespace yfxt
{
    public partial class gl_ypck : Form
    {
        public gl_ypck()
        {
            InitializeComponent();
        }

        public string ckfs;
        string comStr;
        int tijiao_var = 0;               // 变量，控制“提交”按钮是否执行代码
        public string chukudanhao;       //传值比 增加按钮 窗体

        #region 增加按钮
        private void button2_Click(object sender, EventArgs e)
        {
            gl_ypckd ypckd = new gl_ypckd();
            ypckd.Owner = this;
            ckfs = comboBox1.Text.ToString();
            ypckd.ShowDialog();
        }
        #endregion

        #region 删除按钮
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        /// <summary>
        /// 刷新函数              ---显示左和右DatagridView
        /// </summary>
        private void xianshi_DatagridView()
        {
            //左边DataGridView
            DataTable dt1 = new DataTable();
            comStr = "select t.ckdh,t.ckbz from YF_cK01 t where t.ckpb=0 and t.yfsb='" + mainform.yfsb + "' and " +
                "t.ckfs=(select t1.cKFS from YF_cKFS t1 where t1.yfsb='" + mainform.yfsb + "' and t1.fsmc='" + comboBox1.Text + "')";
            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView1.Rows.Clear();
            if (dt1.Rows.Count > 0)
            {
                dataGridView1.Rows.Add(dt1.Rows.Count);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = dt1.Rows[i][0].ToString();
                    dataGridView1.Rows[i].Cells[1].Value = dt1.Rows[i][1].ToString();
                }
            }

            //右边DataGridView
            DataTable dt2 = new DataTable();
            comStr = "select t.ckdh,t.ckrq,t.ckbz from YF_cK01 t where t.ckpb=1 and t.yfsb='" + mainform.yfsb + "' and " +
                "t.ckfs=(select t1.cKFS from YF_cKFS t1 where t1.yfsb='" + mainform.yfsb + "' and t1.fsmc='" + comboBox1.Text + "')";
            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView2.Rows.Clear();
            if (dt2.Rows.Count > 0)
            {
                dataGridView2.Rows.Add(dt2.Rows.Count);
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    dataGridView2.Rows[i].Cells[0].Value = dt2.Rows[i][0].ToString();
                    dataGridView2.Rows[i].Cells[1].Value = Convert.ToDateTime(dt2.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView2.Rows[i].Cells[2].Value = dt2.Rows[i][2].ToString();
                }
            }

            #region datagridView默认不选中单元格，使之成蓝色
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = false;
            }
            if (dataGridView2.Rows.Count > 0)
            {
                dataGridView2.Rows[0].Selected = false;
            }
            #endregion
        }

        private void gl_ypck_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView2.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView2.AllowUserToResizeColumns = false;
            foreach (DataGridViewColumn c in dataGridView1.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            foreach (DataGridViewColumn c in dataGridView2.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            //出库方式
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            DataTable dt = new DataTable();
            comStr = "select fsmc from YF_CKFS where yfsb=" + mainform.yfsb + "";
            dt.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox1.Items.Add(dt.Rows[i][0]);
            }
            comboBox1.Text = dt.Rows[3][0].ToString();

            xianshi_DatagridView();
        }

        #region 删除按钮
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (MessageBox.Show("是否删除出库单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", 
                    "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    string Delete_ckdh = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    try
                    {
                        //删除两张表 ---出库表和出库明细表
                        string comStr = "delete from YF_cK01 where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "';" +
                            "delete from YF_cK02 where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                        xianshi_DatagridView();
                        MessageBox.Show("删除出库单" + "" + Delete_ckdh + "" + "成功！");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
        }
        #endregion  

        #region 提交按钮
        private void button4_Click(object sender, EventArgs e)
        {
            if (tijiao_var == 1)
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    if (MessageBox.Show("是否提交出库单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            //更新，确认出库标识
                            comStr = "update YF_cK01 set cKPB='1',fhy='" + Program.ygbh + "' where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                            //更新，减yf_kcmx的库存ypsl
                            comStr = "update t1 set t1.ypsl=t1.ypsl-t2.cksl from yf_kcmx t1,YF_CK02 t2 where " +
                                "t1.ypxh=t2.ypxh and t1.yfsb=t2.yfsb and t1.ypph=t2.ypph and t2.ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);

                            xianshi_DatagridView();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                    else
                    {
                        tijiao_var = 0;
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {

                            dataGridView1.Rows[i].Selected = false;
                        }
                    }
                }
            }
        }
        #endregion   

        /// <summary>
        /// 刷新按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region 刷新按钮
        private void button1_Click(object sender, EventArgs e)
        {
            xianshi_DatagridView();
        }
        #endregion

        private void button5_Click(object sender, EventArgs e)
        {
            chukudanhao = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            gl_ypckd_you ypckd_you = new gl_ypckd_you();
            ypckd_you.Owner = this;
            ypckd_you.ShowDialog();
        }
        #region 显示出库单
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            chukudanhao = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            gl_ypckd_zuo ypckd_zuo = new gl_ypckd_zuo();
            ypckd_zuo.Owner = this;
            ypckd_zuo.ShowDialog();
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            chukudanhao = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            gl_ypckd_you ypckd_you = new gl_ypckd_you();
            ypckd_you.Owner = this;
            ypckd_you.ShowDialog();
        }
        #endregion

        //变量，控制“提交”按钮是否执行代码
        private void dataGridView1_Enter(object sender, EventArgs e)
        {
            tijiao_var = 1;
        }

        #region datagridView失去焦点，单元格不显示蓝色
        private void dataGridView1_Leave(object sender, EventArgs e)
        {
            if (!button4.Focused)
            {
                tijiao_var = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    dataGridView1.Rows[i].Selected = false;
                }
            }

            //方法2：
            //this.dataGridView1.ClearSelection();
            //this.dataGridView1.TabStop = false;

            //方法3：this.dataGridView1.CurrentCell = null;
        }

        private void dataGridView2_Leave(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {

                dataGridView2.Rows[i].Selected = false;
            }
        }
        #endregion   

        //按出库方式，显示左、右datagridView
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            xianshi_DatagridView();
        }

        //窗体重新获取焦点，发生的刷新事件
        private void gl_ypck_Paint(object sender, PaintEventArgs e)
        {
            xianshi_DatagridView();
        }
    }
}
